package com.zadtek.shopmall.activities;

import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import com.wakehao.bar.BottomNavigationBar;
import com.wakehao.bar.BottomNavigationItemWithDot;
import com.zadtek.shopmall.R;
import com.zadtek.shopmall.base.AbsActivity;
import com.zadtek.shopmall.bean.UpdateVersionBean;
import com.zadtek.shopmall.fragments.HomeFragment;
import com.zadtek.shopmall.fragments.MeFragment;
import com.zadtek.shopmall.fragments.PublishInfoFragment;
import com.zadtek.shopmall.fragments.ShopCartFragment;
import com.zadtek.shopmall.fragments.TypeFragment;
import com.zadtek.shopmall.net.BaseObserver;
import com.zadtek.shopmall.net.BaseResponse;
import com.zadtek.shopmall.net.NetTool;
import com.zadtek.shopmall.other.DialogManger;

import java.util.LinkedList;
import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class MainActivity extends AbsActivity {
    private static final String TAG = "MainActivity";
    private List<Fragment> fragments = new LinkedList<>();
    private FragmentManager fragmentManager;
    private BottomNavigationBar bottomNavigationBar;
    private Fragment mFragmentContent;
    public final String PACKGE_NAME = "com.zadtek.shopmall";
    private ImageView car;

    @Override
    public int setView() {
        return R.layout.activity_main;
    }

    @Override
    public void initBasic(Bundle savedInstanceState) {
        initData(savedInstanceState);
        initView();
        setListener();
//        UpdateVersionBean bean = new UpdateVersionBean();
//        bean.setAddress("https://pro-app-qn.fir.im/4a6e47bc97b7ae98feb4e11554421e0c95eeee4e.apk?attname=demo-debug.apk_2.2.3.apk&e=1529053509&token=LOvmia8oXF4xnLh0IdH05XMYpH6ENHNpARlmPc-T:Av6j6JOOvsqNOJLHb03SjXW8XAI=");
//        bean.setDsc("dfdsf");
//        bean.setVersion_number("f");
//        DialogManger.showUpdate(MainActivity.this,bean);
        //checkUpdate();
    }

    private void checkUpdate() {

        NetTool.getApi().checkUpdate().subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<BaseResponse<UpdateVersionBean>>() {
                    @Override
                    public void onData(BaseResponse<UpdateVersionBean> value) {
                        Log.i(TAG, "checkUpdate onData: msg=" + value.msg + " getCode=" + value.getCode());
                        if (value.getCode() == 0 && value.getData() != null) {

                            PackageInfo packageInfo = null;
                            try {
                                packageInfo = getPackageManager().getPackageInfo(PACKGE_NAME, 0);
                            } catch (PackageManager.NameNotFoundException e) {
                                e.printStackTrace();
                            }
                            int versionCode = packageInfo.versionCode;
                            DialogManger.showUpdate(MainActivity.this, value.getData());
                            if (value.getData().getVersioncode() > versionCode) {
                                DialogManger.showUpdate(MainActivity.this, value.getData());
                            }
                        }
                    }
                });
    }


    private void setListener() {
        bottomNavigationBar.setOnNavigationItemSelectedListener(new BottomNavigationBar.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull BottomNavigationItemWithDot item, int selectedPosition) {
                switch (selectedPosition) {
                    case 0:
                        car.setVisibility(View.VISIBLE);
                        switchFragment(fragments.get(0));

                        break;
                    case 1:
                        car.setVisibility(View.GONE);
                        switchFragment(fragments.get(1));
                        break;
                    case 2:
                        car.setVisibility(View.GONE);
                        switchFragment(fragments.get(2));
                        break;
                    case 3:
                        car.setVisibility(View.GONE);
                        switchFragment(fragments.get(3));
                        break;
                    case 4:
                        car.setVisibility(View.GONE);
                        switchFragment(fragments.get(4));
                        break;
                }
                return true;
            }

            @Override
            public void onNavigationItemSelectedAgain(@NonNull BottomNavigationItemWithDot item, int reSelectedPosition) {

            }
        });
    }

    private void initView() {
        bottomNavigationBar = (BottomNavigationBar) findViewById(R.id.navigation);
        car = (ImageView) findViewById(R.id.car);
        car.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                car.setVisibility(View.GONE);
                switchFragment(fragments.get(3));
                bottomNavigationBar.setItemSelected(3,false);
            }
        });
    }

    private void initData(Bundle savedInstanceState) {
        fragmentManager = getSupportFragmentManager();
        fragments.add(new HomeFragment());
        fragments.add(new TypeFragment());
        fragments.add(new PublishInfoFragment());
        fragments.add(new ShopCartFragment());
        fragments.add(new MeFragment());
        if (savedInstanceState == null) setDefaultFragment(fragments.get(0));

    }

    private void setDefaultFragment(Fragment fragment) {
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.add(R.id.main_container, fragments.get(0)).commit();
        mFragmentContent = fragments.get(0);
    }

    private void switchFragment(Fragment fragment) {
        if (mFragmentContent != fragment) {
            FragmentTransaction transaction = fragmentManager.beginTransaction();
            transaction.replace(R.id.main_container, fragment).commit();
            mFragmentContent = fragment;
        }
    }

    public void show(int i) {
        switchFragment(fragments.get(i));
        bottomNavigationBar.setItemSelected(i, false);
    }

}
